//
//  ViewController.m
//  FishHookDemo
//
//  Created by  JasonLee on 2021/9/8.
//

#import "ViewController.h"
#import "fishhook.h"
#import "Person.h"
#import "CaptainHook.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 定义结构体
    struct rebinding log;
    
    // 制定交换的函数,函数名是啥就写啥
    log.name = "NSLog";
    
    // 要交换的新的函数名, c函数的名称就是函数的指针
    log.replacement = myLog;
    
    // 保存原始函数, 运行时,动态获取nslog函数地址,通过取址符,给函数赋值
    log.replaced = (void *)&sys_log;
    
    // 结构体数组
    struct rebinding rebind[1] = {log};
    
    // hook
    rebind_symbols(rebind, 1);
    // Do any additional setup after loading the view.

}

// 保存原始的函数
static void (* sys_log)(NSString * format, ...);

// 交换的新函数
void myLog(NSString *format, ...) {
    format = [format stringByAppendingString:@"\n 上钩了"];
    // 调用系统函数
    sys_log(format);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSLog(@"我点击了");
    Person *p = [Person new];
    [p jump];
    [p sing];
    [Person say];
}

@end
//1. 先声明要hook 哪个类
CHDeclareClass(Person);
// hook没有参数的实力方法
// CHOptimizedMethod1 一个参数
// CHOptimizedMethod2 两个参数
CHOptimizedMethod0(self, void, Person, jump) {
    NSLog(@"hook住了 jump");
    // 调用hook前的方法
    CHSuper0(Person, jump);

}
CHOptimizedMethod0(self, void, Person, sing) {
//    NSString *song = CHSuper0(Person, sing);
    NSLog(@"hook住了 sing");
    // 有返回值
    return CHSuper0(Person, sing);
    
}
CHOptimizedClassMethod0(self, void, Person, say) {
    NSLog(@"hook 住了say");
    CHSuper0(Person, say);
}

CHConstructor {
    CHLoadClass(Person);
    CHClassHook0(Person, jump);
    CHClassHook0(Person, sing);
    CHClassHook0(Person, say);

}
